/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: NewsPostCrawlOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.news.post.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年8月13日 下午3:35:11 
 * @version: V1.0   
 */
package com.zizo.autohome.news.post.crawl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import com.zizo.autohome.news.post.bean.AutoHomeNewsPost;
import com.zizo.autohome.news.post.dao.NewsDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: NewsPostCrawlOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年8月13日 下午3:35:11
 */
public class NewsPostCrawlOfAutoHome extends Crawl {

	/**
	 * @Title:NewsPostCrawlOfAutoHome
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public NewsPostCrawlOfAutoHome(String id, DAO dao) {
		super(id, dao);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		try {
			Elements pages = doc.select(selectStr);
			return Integer.parseInt(pages.get(pages.size() - 2).text());
		} catch (Exception e) {
			return 1;
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		// url= http://www.autohome.com.cn/364/0/0-0-1-0/
		// Document doc = DocUtil.getDoc(url);
		// Elements contents = doc.select("div#auto-channel-lazyload-article
		// ul.article li:not(#ad_tw_04)");
		// for (Element content : contents) {
		// Element span = content.select("span.fn-right em").first();
		// String link = content.select("a").first().attr("href");
		// String lls = span.text();// 浏览数
		// int l = 0;
		// if (lls.contains("万")) {
		// l = (int) Double.parseDouble(lls.split("万")[0]) * 10000;
		// } else {
		// l = Integer.parseInt(lls);
		// }
		// crawlContent(link + "_" + l);
		// }
		try {
			NewsDaoOfAutoHome newsdao = (NewsDaoOfAutoHome) dao;
			Document doc = DocUtil.getDoc(url);
			int pages = getPages(doc, "div.page a");
			for (int i = 1; i < pages + 1; i++) {
				String link = "http://www.autohome.com.cn/" + this.id + "/0/0-0-" + i + "-0/";
				Document docPage = DocUtil.getDoc(link);
				Elements lis = docPage.select("div.cont-info li");
				for (Element li : lis) {
					String lls = li.select("p.name-tx span").get(2).text();
					String pls = li.select("p.name-tx span").get(3).text();
					int xw_lls = Integer.parseInt(lls);
					int xw_pls = Integer.parseInt(pls);
					String linkUrl = li.select("h3 a").attr("href");
					Elements cars = li.select("p.car");
					if (cars.size() > 0) {
						Elements as = cars.select("a");
						for (Element a : as) {
							String name = a.text();
							String urllx = a.attr("href");
							if (urllx.contains("http://sou.autohome.com.cn/")) {
								continue;
							}
							int typeid = Integer.parseInt(urllx.split("/")[3]);
							String newsid = linkUrl.split("/")[5].split(".html")[0].trim();
							int xw_id = Integer.parseInt(newsid);
							Document docnew = DocUtil.getDoc(linkUrl);
							int xw_ppid = Integer.parseInt(this.id);// 品牌ID
							String xw_type = linkUrl.split("/")[3];// 新闻类型
							Elements ppidEles = docnew.select("div.subnav-title-name a");
							if (ppidEles.size() > 0) {
								xw_ppid = Integer.parseInt(ppidEles.first().attr("href").split("/")[1]);
							}
							String xw_pmlx = "";// 排名类型
							int xw_pm = 0;// 排名
							Elements pmEle = docnew.select("div.subnav-title-rank");
							if (pmEle.size() > 0 && !pmEle.select("span.red").text().contains("暂无")) {
								xw_pmlx = pmEle.text().split("排名")[0];
								xw_pm = Integer.parseInt(
										Util.getMatched(pmEle.select("span.red").text(), "(.*)(\\d{1,})").group(2));
							}
							String xw_bt = "";
							if (docnew.select("div#articlewrap h1").size() > 0) {
								xw_bt = docnew.select("div#articlewrap h1").first().text();// 标题
							}
							Elements items = docnew.select("div.article-info span");
							String xw_fbsj = "1990-1-1 00:00";
							if (items.size() > 0) {
								xw_fbsj = items.get(0).text();// 发表时间
							}

							String xw_lx = "";
							if (items.size() > 2) {
								xw_lx = items.get(2).text().split("：")[1];// 类型
							}
							String bjid = "0";
							if (items.size() > 3) {
								if (items.get(3).select("a").size() > 0) {
									bjid = items.get(3).select("a").attr("href").split("_")[1].split("\\.")[0];
								}
							}
							int xw_bj = Integer.parseInt(bjid);// 编辑作者的ID
							String xw_nr;// 评论内容
							String nr = docnew.select("div#articleContent").html();
							OutputSettings settings = new OutputSettings();
							settings.prettyPrint(false);
							xw_nr = Parser.unescapeEntities(Jsoup.clean(nr, "", Whitelist.none(), settings),false).trim();// 评论内容
							AutoHomeNewsPost post = new AutoHomeNewsPost(xw_id, xw_ppid, xw_type, xw_pmlx, xw_pm, xw_bt,
									xw_fbsj, xw_lx, xw_bj, xw_nr, xw_lls);
							newsdao.insertNewsPost(post);
							 newsdao.insertNUM(xw_id, xw_ppid, xw_fbsj, xw_lls, xw_pls);
							 newsdao.insertType(xw_id, typeid, name);
						}

					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		getItems("http://www.autohome.com.cn/" + this.id + "/0/0-0-1-0/");
		System.out.println(this.id+"完成");
	}

//	public static void main(String[] args) {
//		DAO dao = new NewsDaoOfAutoHome(ConnectionPool.getConnection("autohome_other", "carvoice.tpddns.cn", "3308"));
//		 ExecutorService fix = Executors.newFixedThreadPool(1);
//		 List<String> ids = dao.getIDs();
//		 for (String id : ids) {
//		 NewsPostCrawlOfAutoHome post = new NewsPostCrawlOfAutoHome(id, dao);
//		 fix.execute(post);
//		 }
//	}

	@Override
	public void crawlContent(String url) {
		// TODO Auto-generated method stub

	}
}
